package com.lzc.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzc.zhxy.mapper.TbAdminMapper;
import com.lzc.zhxy.pojo.LoginForm;
import com.lzc.zhxy.pojo.TbAdmin;
import com.lzc.zhxy.service.TbAdminService;
import com.lzc.zhxy.util.MD5;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
* @author 82389
* @description 针对表【tb_admin】的数据库操作Service实现
* @createDate 2022-04-16 18:00:49
*/
@Service("tbAdminService")
@Transactional
public class TbAdminServiceImpl extends ServiceImpl<TbAdminMapper, TbAdmin> implements TbAdminService {

    /**
     * 登陆方法
     * @param loginForm
     * @return
     */
    @Override
    public TbAdmin login(LoginForm loginForm) {
        QueryWrapper<TbAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",loginForm.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));

        TbAdmin admin = baseMapper.selectOne(queryWrapper);
        return admin;
    }

    @Override
    public TbAdmin getAdminById(int intValue) {
        QueryWrapper<TbAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",intValue);

        TbAdmin admin = baseMapper.selectOne(queryWrapper);
        return admin;

    }

    @Override
    public IPage<TbAdmin> getAdmins(Page<TbAdmin> pageParam, String adminName) {
        QueryWrapper<TbAdmin> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(adminName)){
            queryWrapper.like("name",adminName);
        }
        queryWrapper.orderByDesc("id");
        queryWrapper.orderByAsc("name");
        Page page = baseMapper.selectPage(pageParam, queryWrapper);
        return page;
    }
}
